package com.mobileares.midp.demo.client;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.mobileares.midp.demo.client.page.Form;
import com.mobileares.midp.demo.client.page.Tab;
import com.mobileares.midp.demo.client.page.Window;
import com.mobileares.midp.widgets.client.frame.Frame;
import com.mobileares.midp.widgets.client.panel.DecoratePanel;
import com.mobileares.midp.widgets.client.tab.VerticalTabPanel;
import com.mobileares.midp.widgets.client.tree.CTree;
import com.mobileares.midp.widgets.client.tree.CTreeItem;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-7-21
 * Time: 15:00:29
 * To change this template use File | Settings | File Templates.
 */
public class MenuBuilder {

    public static Widget getMenuWidget() {
//        int height = DemoDashboard.frame.getMainHeight() - 6-150-15;
        final VerticalTabPanel menu = new VerticalTabPanel(189, 600);
        menu.addTabItem("控件集合", getFunctions());
        menu.addTabItem("实例集合", new Label("建设中"));
        menu.setTabItem(0);
        DemoDashboard.frame.addListener(new Frame.LayoutListener() {
            public void layout(int width, int height) {
                menu.setHeight(height-15);
            }
        });
        return menu;
    }

    private static Widget getFunctions() {
        initMenuDate();
        CTree tree = new CTree();
        tree.addTreeItem(new CTreeItem("按钮"));
        tree.addTreeItem(new CTreeItem("窗口"));
        tree.addTreeItem(new CTreeItem("Panel"));
        tree.addTreeItem(new CTreeItem("TabPanel"));
        tree.addTreeItem(new CTreeItem("列表控件"));
        tree.addTreeItem(new CTreeItem("表单元素"));
//        tree.addTreeItem(new CTreeItem("提示窗口"));
        tree.addSelectionHandler(new SelectionHandler<CTreeItem>() {
            public void onSelection(SelectionEvent<CTreeItem> cTreeItemSelectionEvent) {
                CTreeItem item = cTreeItemSelectionEvent.getSelectedItem();
                String userObject = item.getText();
                if (DemoDashboard.tabPanel.getItem(userObject) != null) {
                } else {
                    DecoratePanel panel = new DecoratePanel();
//                    panel.setHeight(DemoDashboard.frame.getMainHeight() - 40);
                    if (cache.get(userObject) == null)
                        panel.addWidget(new Label(item.getText()));
                    else
                        panel.addWidget(cache.get(userObject));
                    DemoDashboard.tabPanel.addItem(item.getText(), panel);
                }
                DemoDashboard.tabPanel.setSelectItem(userObject);

            }
        });
        return tree;
    }

    static Map<String, Widget> cache = new HashMap<String, Widget>();

    private static void initMenuDate() {
        cache.put("窗口", new Window());
        cache.put("TabPanel", new Tab());
        cache.put("列表控件", new com.mobileares.midp.demo.client.page.List());
        cache.put("Panel", new com.mobileares.midp.demo.client.page.Panel());
        cache.put("按钮", new com.mobileares.midp.demo.client.page.Button());
        cache.put("表单元素", new Form());
    }
}
